package com.hyfrogx.modules.manage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.qcloud.QcloudApiModuleCenter;
import com.hyfrogx.modules.app.utils.VodUtils;
import com.hyfrogx.modules.manage.dao.video.VideoVodInfoDao;
import com.hyfrogx.modules.manage.entity.video.VideoVodInfoEntity;
import com.hyfrogx.modules.manage.service.HyVideoVodInfoService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * @ClassName HyVideoVodInfoServiceImpl
 * @Description TODO
 * @Author yangjun
 * @date 2019/12/4 10:42
 * @Version 1.0
 **/
@Service
public class HyVideoVodInfoServiceImpl extends ServiceImpl<VideoVodInfoDao, VideoVodInfoEntity> implements HyVideoVodInfoService {

    @Override
    public List getConvertVideoInfoListFromQcloud(String fileId) throws Exception {

        List<VideoVodInfoEntity> list = new ArrayList<>();

        QcloudApiModuleCenter module = VodUtils.getConfig();
        TreeMap<String, Object> params = VodUtils.getParam();
        params.put("fileId", fileId);
        params.put("infoFilter.0", "transcodeInfo");
        String result = null;
        result = module.call("GetVideoInfo", params);
        JSONObject object = new JSONObject(result);

        if (object.getInt("code") == 0) {
            JSONObject jarr = object.getJSONObject("transcodeInfo");
            JSONArray arr = jarr.getJSONArray("transcodeList");

            for (int i = 0;i<arr.length();i++){
                JSONObject jsonObj = arr.getJSONObject(i);
                VideoVodInfoEntity vodInfo = new VideoVodInfoEntity();
				// 插入对象
                vodInfo.setUrl(jsonObj.getString("url"));
                vodInfo.setDefinition(jsonObj.getInt("definition"));
                vodInfo.setTime(jsonObj.getInt("duration"));
                int size = jsonObj.getInt("size");
                BigDecimal bg = new BigDecimal((float)size/1024/1024).setScale(2, RoundingMode.UP);
                vodInfo.setSize(bg.floatValue());
                vodInfo.setVbitrate(jsonObj.getInt("bitrate"));
                String md5 = jsonObj.getString("md5");
                vodInfo.setMd5(md5);
                list.add(vodInfo);
            }
        }
        return  list;
    }
}
